#include <Misc/Config.hpp>


Config *Config::configInstance = 0;

Config::Config(){

	setDefault();

}

Config *Config::instance(){
	if(Config::configInstance == 0){
		Config::configInstance = new Config();
	}
	return Config::configInstance;
}

void Config::parseArguments(int argc, char **argv){

	if(argc > 1){

		for(int i=1; i<argc; i++){

			std::string arg(argv[i]);

			if(!arg.compare("-pc") || !arg.compare("--port-client")){

				std::istringstream(argv[i+1]) >> clientPort;

			}else if(!arg.compare("-pm") || !arg.compare("--port-multicast")){

				std::istringstream(argv[i+1]) >> multicastPort;

			}else if(!arg.compare("-pu") || !arg.compare("--port-unicast")){

				std::istringstream(argv[i+1]) >> unicastPort;

			}else if(!arg.compare("-ia") || !arg.compare("--interface-address")){

				std::istringstream(argv[i+1]) >> interfaceAddress;

			}else if(!arg.compare("-ma") || !arg.compare("--multicast-address")){

				std::istringstream(argv[i+1]) >> multicastAddress;

			}else if(!arg.compare("-n") || !arg.compare("--name")){

				std::istringstream(argv[i+1]) >> clientName;

			}else if(!arg.compare("-i") || !arg.compare("--info-file")){

				std::istringstream(argv[i+1]) >> clientInformationFile;

			}else if(!arg.compare("-r") || !arg.compare("--raw-data-log-file")){

				std::istringstream(argv[i+1]) >> rawDataLogFile;

			}else if(!arg.compare("-l") || !arg.compare("--log-file")){

				std::istringstream(argv[i+1]) >> logFile;

			}else if(!arg.compare("-p") || !arg.compare("--peer-discovery-file")){

				std::istringstream(argv[i+1]) >> peerDiscoveryFile;

			}else if(!arg.compare("--input-file")){

				std::istringstream(argv[i+1]) >> clientInputFile;

			}else if(!arg.compare("--output-file")){

				std::istringstream(argv[i+1]) >> clientOutputFile;

			}else if(!arg.compare("--connection-number")){

				std::istringstream(argv[i+1]) >> connectionNumber;

			}else if(!arg.compare("-h") || !arg.compare("--help")){

				helpFlag = true;

			}

		}

	}else{
		/*
		 * Set default values
		 */
		setDefault();
	}


}

void Config::setDefault(){

	interfaceAddress = "::1";
	multicastAddress = "ff15::1";
	clientPort = "5000";
	multicastPort = "7000";
	unicastPort = "7001";
	connectionNumber = 3;

	peerDiscoveryFile = "/tmp/ibrc-peer-discovery.info";
	rawDataLogFile = "/tmp/ibrc-server.raw";
	clientInformationFile = "/tmp/ibrc-client.info";
	logFile = "/tmp/ibrc-server.log";

	helpFlag = false;


}

std::string& Config::getClientName(){
	return clientName;
}

std::string& Config::getInterfaceAddress(){
	return interfaceAddress;
}

std::string& Config::getMulticastAddress(){
	return multicastAddress;
}

int Config::getConnectionNumber(){
	return connectionNumber;
}

std::string& Config::getClientPort(){
	return clientPort;
}

std::string& Config::getMulticastPort(){
	return multicastPort;
}

std::string& Config::getUnicastPort(){
	return unicastPort;
}

std::string& Config::getPeerDiscoveryFile(){
	return peerDiscoveryFile;
}

std::string& Config::getRawDataLogFile(){
	return rawDataLogFile;
}

std::string& Config::getClientInformationFile(){
	return clientInformationFile;
}

std::string& Config::getLogFile(){
	return logFile;
}

std::string& Config::getClientInputFile(){
	return clientInputFile;
}

std::string& Config::getClientOutputFile(){
	return clientOutputFile;
}

bool Config::isHelpFlag(){
	return helpFlag;
}

